package gdr.window;

import java.util.*;
import java.awt.*;
import gdr.dev.*;

/**
 * Contiene dati riguardanti una finestra.
 * Metodi da estendere:
 * 
 * protected void elaboraInput(long elapsedTime) - modifica la gestione dell'input.
 * 	estendere anche keyManager per una gestione ottimale
 * 
 * protected void updateWindow(long elapsedTime) - aggiorna quanto viene visualizzato sulla schermata
 * protected void renderWindow(Graphics2D g) - mostra la schermata
 * 
 * @author Giorgio Vitale
 */
public class BaseWindow {
	protected SuperGDR9000 gdr;
	
	protected ArrayList<BaseWindow> children;
	protected BaseWindow parent;
	
	/** La finestra ha bisogno di essere aggiornata*/
	protected boolean needRefresh;
	/** La finestra ha bisogno di essere mostrata a schermo*/
	protected boolean isVisible;
	/** La finestra ha il focus, consuma quindi i tasti digitati*/
	protected boolean isFocused;
	/** Se impostato a true, aggiorna e mostra a video lo sfondo*/
	protected boolean gestisceBackground;
	
	protected boolean defaultNeedRefresh;
	protected boolean defaultIsVisible;
	protected boolean defaultIsFocused;
	
	/** Sfondo dell'applicazione*/
	protected BaseBackground background;
	
	/** Fornisce un'interfaccia alla tastiera*/
	protected KeyManager keyManager;
	
	/** Titolo della finestra*/
	protected String title;
	
	public BaseWindow(SuperGDR9000 gdr, String title){
		children = new ArrayList<BaseWindow>();
		this.title = title;
		needRefresh = false;
		isVisible = false;
		isFocused = false;
		this.gdr = gdr;
		keyManager = new KeyManager(gdr);
	}
	
	/**
	 * Inizializza la finestra
	 */
	public void init(){
	}
	
	/**
	 * Attiva la finestra
	 */
	public void setWindowVisibility(){
		setWindowVisibility(isDefaultIsFocused());
	}
	
	public void setWindowVisibility(boolean isFocused){
		setWindowVisibility(isDefaultNeedRefresh(), isDefaultIsVisible(), isFocused);
	}
	
	public void setWindowVisibility(boolean isVisible, boolean needRefresh, boolean isFocused){
		setVisible(isVisible);
		setNeedRefresh(needRefresh);
		setFocused(isFocused);
	}
	public void setDefaultWindowVisibility(boolean isVisible, boolean needRefresh, boolean isFocused){
		setDefaultIsVisible(isVisible);
		setDefaultNeedRefresh(needRefresh);
		setDefaultIsFocused(isFocused);
	}
	
	/**
	 * Nasconde la finestra
	 */
	public void hide(){
		isVisible = false;
		needRefresh = false;
		isFocused = false;
	}
	
	public void addChild(BaseWindow w){
		getChildren().add(w);
		w.setParent(this);
	}
	
	/**
	 * Rimuove la finestra dall'albero
	 */
	public void removeWindow(){
		if (getParent()!=null){
			getParent().getChildren().remove(this);
		}
	}
	
	/**
	 * Non estendere questa classe, modificare il metodo refresh per modificare la finestra,
	 * estendere KeyManager per modificare la gestione dell'input
	 * @param elapsedTime
	 */
	public void update(long elapsedTime){
		if (isFocused())
			elaboraInput(elapsedTime);
		if (isNeedRefresh())
			updateWindow(elapsedTime);
		if (isGestisceBackground())
			background.update(elapsedTime);
		for (int i=0; i<children.size(); i++)
			children.get(i).update(elapsedTime);
	}
	
	/**
	 * Importa e consuma l'input da tastiera
	 * @param elapsedTime
	 */
	public void elaboraInput(long elapsedTime){
		keyManager.update(elapsedTime);
	}
	
	/**
	 * Aggiorna la finestra
	 * @param elapsedTime
	 */
	public void updateWindow(long elapsedTime){
		
	}
	
	/**
	 * Aggiorna questa finestra e tutte le figlie. Modificare renderWindow(g) per
	 * impostare la grafica di questa finestra
	 * @param g
	 */
	public void render(Graphics2D g){
		if (isGestisceBackground())
			background.render(g);
		if (isVisible())
			renderWindow(g);
		for (int i=0; i<children.size(); i++)
			children.get(i).render(g);
	}
	
	/**
	 * Stampa la grafica di questa finestra
	 */
	public void renderWindow(Graphics2D g){
		
	}
	
	
	
	
	
	
	public SuperGDR9000 getGdr() {
		return gdr;
	}

	public void setGdr(SuperGDR9000 gdr) {
		this.gdr = gdr;
	}

	public ArrayList<BaseWindow> getChildren() {
		return children;
	}

	public void setChildren(ArrayList<BaseWindow> children) {
		this.children = children;
	}

	public BaseWindow getParent() {
		return parent;
	}

	public void setParent(BaseWindow parent) {
		this.parent = parent;
	}

	public boolean isNeedRefresh() {
		return needRefresh;
	}

	public void setNeedRefresh(boolean needRefresh) {
		this.needRefresh = needRefresh;
	}

	public boolean isVisible() {
		return isVisible;
	}

	public void setVisible(boolean isVisible) {
		this.isVisible = isVisible;
	}

	public boolean isFocused() {
		return isFocused;
	}

	public void setFocused(boolean isFocused) {
		this.isFocused = isFocused;
	}

	public KeyManager getKeyManager() {
		return keyManager;
	}

	public void setKeyManager(KeyManager keyManager) {
		this.keyManager = keyManager;
	}

	public boolean isGestisceBackground() {
		return gestisceBackground;
	}

	public void setGestisceBackground(boolean gestisceBackground) {
		this.gestisceBackground = gestisceBackground;
	}

	public BaseBackground getBackground() {
		return background;
	}

	public void setBackground(BaseBackground background) {
		this.background = background;
	}

	public boolean isDefaultNeedRefresh() {
		return defaultNeedRefresh;
	}

	public void setDefaultNeedRefresh(boolean defaultNeedRefresh) {
		this.defaultNeedRefresh = defaultNeedRefresh;
	}

	public boolean isDefaultIsVisible() {
		return defaultIsVisible;
	}

	public void setDefaultIsVisible(boolean defaultIsVisible) {
		this.defaultIsVisible = defaultIsVisible;
	}

	public boolean isDefaultIsFocused() {
		return defaultIsFocused;
	}

	public void setDefaultIsFocused(boolean defaultIsFocused) {
		this.defaultIsFocused = defaultIsFocused;
	}
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
}
